package com.hunan.api.catv.service.biz.tencent_cloud.im;

import com.google.gson.JsonObject;
import com.hunan.api.catv.common.CacheKey;
import com.hunan.api.catv.common.Constants;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.vo.MemberVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.service.biz.member.MemberBiz;
import com.hunan.api.catv.service.impl.tencent_cloud.im.ImService;
import com.hunan.api.catv.service.impl.tencent_cloud.im.SignService;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.MessageFormat;

@Service
public class ImBiz extends ResultService {

    @Autowired
    SignService signService;

    @Autowired
    CacheBiz cacheBiz;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    ImService imService;

    @Autowired
    MemberBiz memberBiz;

    public ServiceResult getSign(Integer source, Object identifier) {
        String signKey = MessageFormat.format(CacheKey.im_sign, identifier.toString());
        Object signObject = redisUtils.get(signKey);
        if (!ObjectUtils.isEmpty(signObject)) {
            return buildOK(signObject.toString());
        }
        JsonObject imConf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tencent_cloud_im_conf), JsonObject.class);
        if (!ObjectUtils.isEmpty(imConf)) {
            Long appId = imConf.get("appId").getAsLong();
            String key = imConf.get("key").getAsString();
            String account = "";
            if (source == 1) {
                account = identifier.toString();
            }
            if (source == 2) {
                MemberVo memberVo = memberBiz.info(Integer.valueOf(identifier.toString()));
                if (!ObjectUtils.isEmpty(memberVo)) {
                    account = memberVo.getIdentifier();
                }
            }
            String sign = signService.genSig(appId, key, account, 1296000L, null);
            redisUtils.set(signKey, sign, 1296000L);
            return buildOK(sign);
        }
        return buildFail(ConstantsMsg.im_getSign_fail);

    }

    public ServiceResult importAccount(Integer memberId) {
        JsonObject imConf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tencent_cloud_im_conf), JsonObject.class);
        if (!ObjectUtils.isEmpty(imConf)) {
            String domain = imConf.get("domain").getAsString();
            Long appId = imConf.get("appId").getAsLong();
            String identifier = imConf.get("identifier").getAsString();
            MemberVo memberVo = memberBiz.info(memberId);
            if (!ObjectUtils.isEmpty(memberVo)) {
                JsonObject result = imService.importAccount(domain, appId.toString(), identifier, getSign(1, identifier).getData().toString(), memberVo.getIdentifier(), memberVo.getNickName(), memberVo.getAvatarUrl());
                if (!ObjectUtils.isEmpty(result)) {
                    Integer code = result.get("ErrorCode").getAsInt();
                    String msg = result.get("ErrorInfo").getAsString();
                    if (code == 0) {
                        return buildOK(code);
                    }
                    return buildFail(msg);
                }
            }
        }
        return buildFail(ConstantsMsg.im_createAccount_fail);
    }

    public ServiceResult createGroup(String name) {
        JsonObject imConf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tencent_cloud_im_conf), JsonObject.class);
        if (!ObjectUtils.isEmpty(imConf)) {
            String domain = imConf.get("domain").getAsString();
            Long appId = imConf.get("appId").getAsLong();
            String identifier = imConf.get("identifier").getAsString();
            JsonObject result = imService.createGroup(domain, appId.toString(), identifier, getSign(1, identifier).getData().toString(), "AVChatRoom", name);
            if (!ObjectUtils.isEmpty(result)) {
                Integer code = result.get("ErrorCode").getAsInt();
                String msg = result.get("ErrorInfo").getAsString();
                if (code == 0) {
                    String groupId = result.get("GroupId").getAsString();
                    return buildOK(groupId);
                }
                return buildFail(msg);
            }
        }
        return buildFail(ConstantsMsg.im_createGroup_fail);
    }

    public ServiceResult destoryGroup(String groupId) {
        JsonObject imConf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tencent_cloud_im_conf), JsonObject.class);
        if (!ObjectUtils.isEmpty(imConf)) {
            String domain = imConf.get("domain").getAsString();
            Long appId = imConf.get("appId").getAsLong();
            String identifier = imConf.get("identifier").getAsString();
            JsonObject result = imService.destoryGroup(domain, appId.toString(), identifier, getSign(1, identifier).getData().toString(), groupId);
            if (!ObjectUtils.isEmpty(result)) {
                Integer code = result.get("ErrorCode").getAsInt();
                String msg = result.get("ErrorInfo").getAsString();
                if (code == 0) {
                    return buildOK(code);
                }
                return buildFail(msg);
            }
        }
        return buildFail(ConstantsMsg.im_createAccount_fail);
    }
}
